package com.cy.lib.adapter;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * @author CY
 * @title
 * @date 2015年7月2日
 * @qq 568305883
 * @email tmxdyf@163.com
 */
public abstract class BExpandableAdapter<TGroup, TChild> extends BaseExpandableListAdapter {

    private Activity mAct;
    LayoutInflater mInflater;
    // 每组数据，
    private List<TGroup> mListGroup;
    // 每组中Item数据
    private List<List<TChild>> mListChildGroup;

    public BExpandableAdapter(Activity act) {
        mAct = act;
        mInflater = mAct.getLayoutInflater();
        mListChildGroup = new ArrayList<>();
        mListGroup = new ArrayList<>();
    }

    public List<List<TChild>> getListChildGroup() {
        return mListChildGroup;
    }

    public List<TGroup> getListGroup() {
        return mListGroup;
    }

    public TGroup getListGroup(int groupPosition) {
        return getListGroup().get(groupPosition);
    }

    public List<TChild> getListChildGroup(int groupPosition) {
        return getListChildGroup().get(groupPosition);
    }

    public TChild getListChildGroup(int groupPosition, int childPosition) {
        return getListChildGroup().get(groupPosition).get(childPosition);
    }

    /**
     * 在指定组中添加Item
     *
     * @param groupPosition
     * @param t
     */
    public void addChildItem(int groupPosition, TChild t, boolean isNotify) {
        if (groupPosition < mListChildGroup.size() && groupPosition >= 0) {
            mListChildGroup.get(groupPosition).add(t);
        }
        if (isNotify) {
            notifyDataSetChanged();
        }
    }

    /**
     * 添加一个分组
     *
     * @param t
     * @param listChild
     */
    public void addGroupItem(TGroup t, List<TChild> listChild) {
        mListGroup.add(t);
        if (listChild != null) {
            mListChildGroup.add(listChild);
        }
        notifyDataSetChanged();
    }

    /***********************************/

    @Override
    public int getGroupCount() {
        return mListChildGroup.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mListChildGroup.get(groupPosition).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return mListChildGroup.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return mListChildGroup.get(groupPosition).get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public abstract View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent);

    @Override
    public abstract View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent);

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
    }

}
